package com.tianwen.management.controller;

import com.tianwen.management.entity.Department;
import com.tianwen.management.entity.User;
import com.tianwen.management.result.Result;
import com.tianwen.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: personnel-management
 * @description
 * @author: tianwen
 * @create: 2023-05-17 09:04
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/page/{page}/{pageSize}")
    public Result page(@PathVariable Integer page, @PathVariable Integer pageSize, String username){
        List<User> userList=userService.page(page, pageSize, username);
        return Result.ok().data("userList",userList);
    }
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('用户增删改')")
    public Result add(@RequestBody User user){
        userService.saveAndRole(user);
        return Result.ok();
    }
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('用户增删改')")
    public Result delete(Long id){
        if (userService.removeAndRole(id)) {
            return Result.ok();
        }else{
            return Result.error();
        }


    }
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('用户增删改')")
    public Result update(@RequestBody User user){
        if (userService.updateAndRole(user)) {
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        Map map=userService.login(user);
        return Result.ok().data(map);
    }
    @GetMapping("/logout")
    public Result logout() {

        if(userService.logout()){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

}

